<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>splin3d</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : 11/03/2005</div>
    <p>
      <b>splin3d</b> - spline gridded 3d interpolation</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>tl = splin3d(x, y, z, v, [order])</tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>x,y,z</b>
        </tt>: strictly increasing row vectors (each with at least 3 components)
               defining the 3d interpolation grid</li>
      <li>
        <tt>
          <b>v</b>
        </tt>: nx x ny x nz hypermatrix (nx, ny, nz being the length of <tt>
          <b>x</b>
        </tt>,
               <tt>
          <b>y</b>
        </tt> and <tt>
          <b>z</b>
        </tt>)</li>
      <li>
        <tt>
          <b>order</b>
        </tt>: (optional) a 1x3 vector [kx,ky,kz] given the order of the tensor 
               spline in each direction (default [4,4,4], i.e. tricubic spline)</li>
      <li>
        <tt>
          <b>tl</b>
        </tt>: a tlist of type splin3d defining the spline</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
    This function computes a 3d tensor spline <em>s</em> which interpolates the 
    <em>(xi,yj,zk,vijk)</em> points, ie, we have <em>s(xi,yj,zk)=vijk</em> for all  
    <em>i=1,..,nx</em>, <em>j=1,..,ny</em> and   <em>k=1,..,nz</em>.
    The resulting spline <em>s</em> is defined by <tt>
        <b>tl</b>
      </tt> which consists
    in a B-spline-tensor representation of  <em>s</em>.
    The evaluation of  <em>s</em> at some points must be done by the <a href="interp3d.htm">
        <tt>
          <b>interp3d</b>
        </tt>
      </a>
    function (to compute <em>s</em> and its first derivatives) or by the  <a href="bsplin3val.htm">
        <tt>
          <b>bsplin3val</b>
        </tt>
      </a> 
    function (to compute an arbitrary derivative of <em>s</em>) . Several kind of splines may be computed 
    by selecting the order of the spline in each direction <tt>
        <b>order=[kx,ky,kz]</b>
      </tt>.
    </p>
    <h3>
      <font color="blue">Remark</font>
    </h3>
    <dl>
      <p>
       This function works under the conditions: 
    </p>
      <pre>
       nx, ny, nz &gt;= 3
       2 &lt;= kx &lt; nx
       2 &lt;= ky &lt; ny
       2 &lt;= kz &lt; nz</pre>
      <p>
       an error being issued when they are not respected.
    </p>
    </dl>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>
// example 1
func =  "v=cos(2*%pi*x).*sin(2*%pi*y).*cos(2*%pi*z)";
deff("v=f(x,y,z)",func);
n = 10;  // n x n x n  interpolation points
x = linspace(0,1,n); y=x; z=x; // interpolation grid
[X,Y,Z] = ndgrid(x,y,z);
V = f(X,Y,Z);
tl = splin3d(x,y,z,V,[5 5 5]);
m = 10000;
// compute an approximated error
xp = grand(m,1,"def"); yp = grand(m,1,"def"); zp = grand(m,1,"def");
vp_exact = f(xp,yp,zp);
vp_interp = interp3d(xp,yp,zp, tl);
er = max(abs(vp_exact - vp_interp))
// now retry with n=20 and see the error


// example 2 (see linear_interpn help page which have the
//            same example with trilinear interpolation)
getf("SCI/demos/interp/interp_demo.sci") 
func =  "v=(x-0.5).^2 + (y-0.5).^3 + (z-0.5).^2";
deff("v=f(x,y,z)",func);
n = 5; 
x = linspace(0,1,n); y=x; z=x;
[X,Y,Z] = ndgrid(x,y,z);
V = f(X,Y,Z);
tl = splin3d(x,y,z,V);
// compute (and display) the 3d spline interpolant on some slices
m = 41;
dir = ["z="  "z="  "z="  "x="  "y="];
val = [ 0.1   0.5   0.9   0.5   0.5];
ebox = [0 1 0 1 0 1];
XF=[]; YF=[]; ZF=[]; VF=[];
for i = 1:length(val)
   [Xm,Xp,Ym,Yp,Zm,Zp] = slice_parallelepiped(dir(i), val(i), ebox, m, m, m);
   Vm = interp3d(Xm,Ym,Zm, tl);
   [xf,yf,zf,vf] = nf3dq(Xm,Ym,Zm,Vm,1);
   XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf]; VF = [VF vf]; 
   Vp = interp3d(Xp,Yp,Zp, tl);
   [xf,yf,zf,vf] = nf3dq(Xp,Yp,Zp,Vp,1);
   XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf]; VF = [VF vf]; 
end
nb_col = 128;
vmin = min(VF); vmax = max(VF);
color = dsearch(VF,linspace(vmin,vmax,nb_col+1));
xset("colormap",jetcolormap(nb_col));
xbasc(); xset("hidden3d",xget("background"));
colorbar(vmin,vmax)
plot3d(XF, YF, list(ZF,color), flag=[-1 6 4])
xtitle("3d spline interpolation of "+func)
xselect()
 </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="linear_interpn.htm">
        <tt>
          <b>linear_interpn</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="interp3d.htm">
        <tt>
          <b>interp3d</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="bsplin3val.htm">
        <tt>
          <b>bsplin3val</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
    <h3>
      <font color="blue">Authors</font>
    </h3>
    <dl>
      <dd>
        <b></b> R.F. Boisvert, C. De Boor (code from the CMLIB fortran lib)</dd>
      <dd>
        <b></b> B. Pincon (scilab interface)</dd>
    </dl>
  </body>
</html>
